import { action, observable } from 'mobx'
import { getUserInfo,getAuthCode,changeMobile,changeInfo} from '../../server/my'

class my{
  //声明属性
  @observable
  str = {}

  @observable
  phone = ''

  @observable
  code = ''

  @observable
  num = 60



  //方法
  @action
  async getInfo() {
    let result = await getUserInfo();
    let obj=result.data
    let num = result.data.mobile
    let nums=num.slice(4,8)
    num=num.replace(nums,'****')
      obj={
          ...obj,
          mobile:num
      }
    this.str = obj
    console.log(this.str)
  }
    @action
    async getMyPhone(phone:string) {
        this.phone = phone
        let codes = await getAuthCode(phone)
        let time = setInterval(() => {
            this.num--
            if (this.num === 0) {
                clearInterval(time)
                this.num = 60
            }
        }, 1000)
        this.code = codes.data.authCode
  }
  
  @action
  async setPhone(code:string){
      if(this.code===code){
          let obj={
              mobile: this.phone,
              authCode:this.code,
              mid:'fa291cbd52aa40d2b47ba96c8ec59c99',
          }
          console.log(obj);
          let arr = await changeMobile(obj)
          console.log(arr);
      }
  }

  @action setName(name:string){
      let obj={
          name:name,
          mid:'fa291cbd52aa40d2b47ba96c8ec59c99',
      }
      changeInfo(obj)
  }

}
export default my;